package com.huijutao.fragment;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.huijutao.CustomView.WebViewSetting;
import com.huijutao.MyApp;
import com.huijutao.R;
import com.huijutao.beans.HuihuiContentdata;
import com.huijutao.callback.StringDataCallBack;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link com.huijutao.fragment.HuiContentContent.HuiContentContentCallBack} interface
 * to handle interaction events.
 * Use the {@link HuiContentContent#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HuiContentContent extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    private HuihuiContentdata datas;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private String mParam3;
    private HuiContentContentCallBack mListener;
    private View v;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HuiContentContent.
     */
    // TODO: Rename and change types and number of parameters
    public static HuiContentContent newInstance(String param1, String param2, String param3) {
        HuiContentContent fragment = new HuiContentContent();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        args.putString(ARG_PARAM3, param3);
        fragment.setArguments(args);
        return fragment;
    }

    public HuiContentContent() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
            mParam3 = getArguments().getString(ARG_PARAM3);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_hui_content_content, container, false);
       TextView textTitle= (TextView) v.findViewById(R.id.huihui_content_title);
       TextView textPrice= (TextView) v.findViewById(R.id.huihui_content_price);
        TextView textTime= (TextView) v.findViewById(R.id.huihui_content_time);
        textTitle.setText(mParam2);
        textPrice.setText(mParam3);
        MyApp.mStringUtils.getDataJson(mParam1, new StringDataCallBack() {
            @Override
            public void stringDataCallBack(String data) {
                try {
                    JSONObject jsob = new JSONObject(data);
                    JSONObject jsob2 = jsob.getJSONObject("data");
//                    jsob2.getString("share_link")
//                    datas.setPurchase_url(jsob2.getString("purchase_url"));

                    ImageView image= (ImageView) v.findViewById(R.id.huihui_content_image);
                    MyApp.mImageXutils.display(image,jsob2.getString("image_url"));

                    WebView webView = (WebView) v.findViewById(R.id.huihui_content_webview);
                    WebViewSetting.setWebView(webView);
                    webView.loadDataWithBaseURL(null, jsob2.getString("page"), "text/html", "UTF-8", null);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });




        return v;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.huiContentContentCallBack(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (HuiContentContentCallBack) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface HuiContentContentCallBack {
        // TODO: Update argument type and name
        public void huiContentContentCallBack(Uri uri);
    }

}
